package edu.mis.modules.blog;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

import edu.mis.core.annotation.Permission;
import edu.mis.model.Blog;
@Before(BlogInterceptor.class)
public class BlogController extends Controller {
	@Permission("blog:view")
	public void index() {
		render("blog.html");
	}
	
	public void list() {
		Integer page = getParaToInt("page");
		Integer rows = getParaToInt("rows");
		Page<Blog> data = Blog.dao.paginate(page, rows);
		setAttr("rows", data.getList());
		setAttr("total", data.getTotalRow());
		renderJson();
	}
	
	public void add() throws Exception {
		Model<Blog> blog = getModel(Blog.class,"");
		if (blog.save()) {
			setAttr("success",true);
			setAttr("msg","添加博客成功!");
		}else{
			setAttr("success",false);
			setAttr("msg","添加博客失败!");
		}
		renderJson();
	}
	
	public void edit() throws Exception {
		Model<Blog> blog = getModel(Blog.class,"");
		if (blog.update()) {
			setAttr("success",true);
			setAttr("msg","修改博客成功!");
		}else{
			setAttr("success",false);
			setAttr("msg","修改博客失败!");
		}
		renderJson();
	}
	public void delete() throws Exception {
		String ids=getPara("ids");
		if(Blog.dao.deleteBatch(ids)) {
			setAttr("success",true);
			setAttr("msg","删除博客成功!");
		}else{
			setAttr("success",false);
			setAttr("msg","删除博客失败!");
		}
		renderJson();
	}
}
